package com.sghj.utils;

import com.sghj.SysPermission.entity.SysUser;
import com.sghj.config.handler.ResultCode;
import com.sghj.config.handler.exceptionHandler.MyException;
import org.apache.shiro.SecurityUtils;

/**
 * @author xrx
 * @Date 2023/3/15
 * @apiNote
 */
public class UserUtils {

    /**
     * 获取用户
     **/
    public static SysUser getLoginUser() {
        //try {
        //    return (SysUser) SecurityUtils.getSubject().getPrincipal();
        //} catch (Exception e) {
        //    throw new MyException(ResultCode.NEEDRELOGIN,"获取用户信息异常,请重新登录");
        //}
        SysUser user = null;
        user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        if (user==null){
            throw new MyException(ResultCode.NEEDRELOGIN,"获取用户信息异常,请重新登录");
        }
        return user;
    }
    public static SysUser getLoginUser2() {
        //try {
        //    return (SysUser) SecurityUtils.getSubject().getPrincipal();
        //} catch (Exception e) {
        //    throw new MyException(ResultCode.NEEDRELOGIN,"获取用户信息异常,请重新登录");
        //}
        SysUser user = null;
        user = (SysUser) SecurityUtils.getSubject().getPrincipal();
//        if (user==null){
//            throw new MyException(ResultCode.NEEDRELOGIN,"获取用户信息异常,请重新登录");
//        }
        return user;
    }

    /**
     * 获取用户账户
     **/
    public static String getUsername() {
        try {
            return getLoginUser().getSysUserName();
        } catch (Exception e) {
            throw new MyException(ResultCode.NEEDRELOGIN,"获取用户信息异常,请重新登录");

        }
    }

}
